import { v4 as uuidv4 } from 'uuid';

import type { DBAnswer } from '../../types/answer.d';
import { Seeder } from '../../umzug';
import {
  answeringUser,
  anwserCompletedUser,
  beyondTotalNumberOfQuestionsUser
} from './20211003182031-sample-user';
import { TOTAL_QUESTION } from '../../../../constants/answer-progress-state';

const answersAmountArr = [{
  mergeUserQuestionnaireId: answeringUser.id,
  amount: 10
}, {
  mergeUserQuestionnaireId: anwserCompletedUser.id,
  amount: 62
}, {
  mergeUserQuestionnaireId: beyondTotalNumberOfQuestionsUser.id,
  amount: 64
}];

const seedAnwsers: DBAnswer[] = [];
let id = 0;
answersAmountArr.forEach((answersAmount) => {
  const { mergeUserQuestionnaireId, amount } = answersAmount;
  let score = 0;

  for (let questionId = 1; questionId <= amount; questionId += 1) {
    id += 1;
    score += 1;
    score = (score % 5) + 1;

    seedAnwsers.push({
      id,
      uuid: uuidv4(),
      fk_merge_user_questionnaire: mergeUserQuestionnaireId,
      fk_question: questionId <= TOTAL_QUESTION ? questionId : TOTAL_QUESTION,
      score,
      created_at: new Date(),
      updated_at: new Date()
    });
  }
});

export const up: Seeder = async ({ context: sequelize }) => {
  await sequelize
    .getQueryInterface()
    .bulkInsert('answers', seedAnwsers);
};

export const down: Seeder = async ({ context: sequelize }) => {
  await sequelize.getQueryInterface().bulkDelete('answers',
    { id: seedAnwsers.map((u) => u.id) });
};
